<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增角色')" />
	<th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
	<form class="form-horizontal m" id="form-role-add">
		<div class="form-group">
			<label class="col-sm-3 control-label is-required">角色名称：</label>
			<div class="col-sm-8">
				<input class="form-control" type="text" name="roleName" id="roleName" required>
				<input  type="hidden" name="areaId" id="areaId" value="0">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label">所属系统：</label>
			<div class="col-sm-8">
				<select id="platformId" name="platformId" class="form-control m-b" onchange="getAuthority()">
					<option  th:each="platform : ${platforms}"  th:value="${platform.platformId}"  th:text="${platform.name}"></option>
				</select>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label is-required">权限字符：</label>
			<div class="col-sm-8">
				<input class="form-control" type="text" name="roleKey" id="roleKey" required>
				<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 控制器中定义的权限字符，如：@RequiresRoles("")</span>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label is-required">显示顺序：</label>
			<div class="col-sm-8">
				<input class="form-control" type="text" name="roleSort" id="roleSort" required>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label">状态：</label>
			<div class="col-sm-8">
				<label class="toggle-switch switch-solid">
					<input type="checkbox" id="status" checked>
					<span></span>
				</label>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label">备注：</label>
			<div class="col-sm-8">
				<input id="remark" name="remark" class="form-control" type="text">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label">菜单权限：</label>
			<div class="col-sm-8">
				<label class="check-box">
					<input type="checkbox" value="1">展开/折叠</label>
				<label class="check-box">
					<input type="checkbox" value="2">全选/全不选</label>
				<label class="check-box">
					<input type="checkbox" value="3" checked>父子联动</label>
				<div id="menuTrees" class="ztree ztree-border"></div>
			</div>
		</div>
	</form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: ztree-js" />
<script type="text/javascript">
	$(function() {
		getAuthority();
	});


	$('input').on('ifChanged', function(obj){
		var type = $(this).val();
		var checked = obj.currentTarget.checked;
		if (type == 1) {
			if (checked) {
				$._tree.expandAll(true);
			} else {
				$._tree.expandAll(false);
			}
		} else if (type == "2") {
			if (checked) {
				$._tree.checkAllNodes(true);
			} else {
				$._tree.checkAllNodes(false);
			}
		} else if (type == "3") {
			if (checked) {
				$._tree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
			} else {
				$._tree.setting.check.chkboxType = { "Y": "", "N": "" };
			}
		}
	})

	var isClick = true;
	function submitHandler() {
		if ($.validate.form() && isClick) {
			isClick = false;
			add();
		}
	}


	function getAuthority() {
		var url = ctx + 'sso/menu/roleMenuTreeData?platformId='+  $("#platformId").val();
		var options = {
			id: "menuTrees",
			url: url,
			check: { enable: true },
			expandLevel: 0
		};
		$.tree.init(options);
	}


	function add() {
		var platformId = $("#platformId").val();
		var roleName = $("input[name='roleName']").val();
		var roleKey = $("input[name='roleKey']").val();
		var roleSort = $("input[name='roleSort']").val();
		var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
		var remark = $("input[name='remark']").val();
		var areaId = $("input[name='areaId']").val();
		debugger;
		var menuIds = $.tree.getCheckedNodes();
		var staff = {
			"platformId": platformId,
			"roleName": roleName,
			"roleKey": roleKey,
			"roleSort": roleSort,
			"status": status,
			"remark": remark,
			"areaId": areaId,
			"menuIds": menuIds
		}
		$.ajax({
			cache : true,
			type : "POST",
			url :  "/sso/role/add",
			headers: {
				'Authorization': "",
			},
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify(staff),
			dataType: 'json',
			error : function(request) {
				$.modal.alertError("系统错误");
			},
			success : function(data) {
				$.operate.successCallback(data);
			}
		});
	}
</script>
</body>
</html>
